import React, { Component } from 'react'
import Header from "../../components/Header";
import {Cartadd, Getgoodsinfo } from "../../http/api"
import Details from './details/details';
import qs from "querystring"
import { Toast } from 'antd-mobile';

export default class detail extends Component {
    constructor() {
        super()
        this.state = {
            info: {}
        }
    }

    //点击加入购物车
    changeBtn(){
        console.log(this.props);
        let search = this.props.location.search//?
        console.log(search);
        console.log(search.slice(1));
        let type = qs.parse(search.slice(1))//{id:"1"}
        //发请求
        Cartadd({
            uid:JSON.parse(sessionStorage.getItem("info")).uid,
            type:type.type,
            goodsid:type.id,
            num:1
        }).then(res=>{
            if(res.data.code===200){
                Toast.info(res.data.msg)
            }
        })
    }

    async componentDidMount() {
        console.log(this.props);
        //获取id
        let search = this.props.location.search

        let d = qs.parse(search.slice(1));//{id:"1",type:2}
        let res = await Getgoodsinfo({
            id:d.id
        })
        this.setState({
            info: res.data.list[0]
        })
    }
    render() {
        let { info,changeBtn } = this.state
        return (
            <div>
                <Header title="商品详情" back></Header>
                <Details info={info} changeBtn={this.changeBtn.bind(this)}></Details>
            </div>
        )
    }
}
